{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台-商品分类列表</title>
{% endblock %}

{% block content %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf" style="font-size: 26px;color: #3599d6;">商品分类列表</div></div>
                <div class="widget-body  am-fr">
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-9">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button type="button" class="am-btn am-btn-default am-btn-success">
                                        <a href="{% url 'myadmin_cates_add' %}" style="color: #fff">
                                            <i class="iconfont icon-add-s" style="font-size: 12px;"></i>新增</a>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <form>
                        <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                            <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                <input value="{{ request.GET.keywords }}" name="keywords" type="text" class="am-form-field ">
                                <span class="am-input-group-btn">
                                    <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
                                </span>
                            </div>
                        </div>
                    </form>
                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>分类名</th>
                                    <th>所属分类</th>
                                    <th>path</th>
                                    <!-- <th>状态</th> -->
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% load pagetag %}
                                {% for u in cateslist %}
                                <tr class="gradeX">
                                    <td class="am-text-middle">{{ u.id }}</td>
                                    <td class="am-text-middle editname">
                                        <span>{% indent u.path %}</span>
                                        <span class="name">{{ u.name }}</span>
                                    </td>
                                    <td class="am-text-middle">{{ u.pid }}</td>
                                    <td class="am-text-middle">{{ u.path }}</td>
                                    <!-- <td class="am-text-middle">
                                        {% if u.isDelete %}
                                            删除
                                        {% else %}
                                            正常
                                        {% endif %}
                                    </td> -->
                                    <td class="am-text-middle">
                                        <div class="tpl-table-black-operation">
                                            <a href="javascript:void(0)" class="tpl-table-black-operation-del">
                                                <i class="am-icon-trash"></i>删除</a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                                <!-- more data -->
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                            {% load pagetag %}

                            {% showpages cateslist.paginator.num_pages request %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    // 给td绑定双击事件,进行修改分类名
    $('tbody .editname').dblclick(function(){
        // 获取当前的分类名
        var name = $(this).find('.name').text()
        var cid = $(this).prev().text()
        // 动态创建input,放到name的位置
        var inp = $('<input value="'+name+'" style="color:black;">')
        $(this).find('.name').html(inp)
        inp.select()
        var t = $(this)

        // 给input绑定丧失焦点事件
        inp.blur(function(){
            // 获取当前input中修改的值
            var newname = $(this).val()
            // 判断新的值和原来的值是否一致
            if( newname == name ){
                // 恢复原样
                t.find('.name').html(name)
            }else{
                // 发送ajax请求去修改
                $.get('{% url "myadmin_cates_edit" %}',{'cid':cid,'name':newname},function(data){
                    // 恢复原样
                    t.find('.name').html(newname)
                    alert(data['msg'])
                },'json')
            }
        })
    })

    // 给删除按钮绑定单击事件
    $('.tpl-table-black-operation-del').click(function(){
        var cid = $(this).parents('tr').find('td:eq(0)').text()
        var a = $(this)
        // 发送ajax请求
        $.get('{% url "myadmin_cates_del" %}',{'cid':cid},function(data){
            // 判断是否已经删除成功
            if (data['error'] == 0){
                // 删除成功,动态的删除当前这一条分类数据
                a.parents('tr').remove()
            }
            alert(data['msg'])
        },'json')
    })
</script>
{% endblock %}